package com.shj.em.biz.company.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.shj.em.base.controller.BaseController;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;
import com.shj.em.biz.company.dto.CompanyEditInitDTO;
import com.shj.em.biz.company.dto.CompanyInitDTO;
import com.shj.em.biz.company.dto.CompanySearchDTO;
import com.shj.em.biz.company.entity.Company;
import com.shj.em.biz.company.entity.Contact;
import com.shj.em.biz.company.service.CompanyService;
import com.shj.em.biz.sys.entity.Cd;
import com.shj.em.biz.sys.service.CdService;
import com.shj.em.common.annotation.Log;
import com.shj.em.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * <p>
 * 客户信息 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-05
 */
@RestController
@Slf4j
@RequestMapping("/company")
public class CompanyController extends BaseController<Company>{

    public static int CUSTOMER = 1;
    public static int SUPPLY = 2;

    @GetMapping("/loadInitPageData")
    public ResMsg<CompanyInitDTO> loadInitPageData(){
        List<Cd> supplyTypes = cdService.queryCdsByType(CdService.CD_TYPE_SUPPLY_TYPE);
        List<Cd> supplyGrades = cdService.queryCdsByType(CdService.CD_TYPE_SUPPLY_GRADE);

        CompanyInitDTO initDTO = new CompanyInitDTO();
        initDTO.setSupplyGrades(supplyGrades);
        initDTO.setSupplyTypes(supplyTypes);
        return ResMsg.ok(initDTO);
    }

    @RequestMapping("/list")
    public PageVO<Company> list(CompanySearchDTO searchDTO){
        boolean isManager = ShiroUtils.isManager();
        boolean isSuperAdmin = ShiroUtils.isSuperAdmin();
        searchDTO.setSellId(isManager || isSuperAdmin ? null : ShiroUtils.getUserId());
        Page<Company> page = companyService.queryCompanyPage(searchDTO);
        PageVO<Company> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @Log("批量删除公司信息")
    @DeleteMapping
    public ResMsg batchRemove(@RequestBody Long[] ids){
        companyService.deleteBatchIds(Arrays.asList(ids));
        return ResMsg.ok();
    }

    @GetMapping("/{id}")
    public ResMsg<CompanyEditInitDTO> findCompanyById(@PathVariable("id") Long id){
        boolean isEdit = id > -1L;
        Company company = isEdit ? companyService.selectById(id) : new Company();
        CompanyEditInitDTO initDTO = new CompanyEditInitDTO();
        initDTO.setCompany(company);
        initDTO.setContacts(contactService.findContactsByCompanyId(id));
        return ResMsg.ok(initDTO);
    }

    @Log("创建客户信息")
    @PostMapping
    public ResMsg add(@RequestBody Company company){
        long id = companyService.insertCompany(company);
        return ResMsg.ok(id);
    }

    @Log("更新客户信息")
    @PutMapping
    public ResMsg edit(@RequestBody Company company){
        companyService.updateById(company);
        return ResMsg.ok();
    }


    @GetMapping("/findContactsByCompanyId/{companyId}")
    public ResMsg findContactsByCompanyId(@PathVariable("companyId") Long companyId){
        return ResMsg.ok(contactService.findContactsByCompanyId(companyId));
    }
}

